package com.example.demo.util;

import java.util.List;
import java.util.Set;

public class StringUtil {

	 /**
     * 返回首字母小写
     * @param str
     * @return
     */
    public static String getFirstLow(String str){
        if(str==null ){
            return str;
        }else if(str.length()<2){
            return str.toLowerCase();
        }else {
            return str.substring(0, 1).toLowerCase() + str.substring(1);
        }
    }


    /**
     * ""," ",null  返回true
     * @param str
     * @return
     */
    public static boolean isEmpty(String str){
        return str==null||str.trim().equals("");
    }
    public static boolean notEmpty(String str){
        return !isEmpty(str);
    }

    public static String getSql$LikeStr(String str){
        return "%"+str;
    }
    public static String getSqlLike$Str(String str){
        return str+"%";
    }
    public static String getSql$Like$Str(String str){
        return "%"+str+"%";
    }

    /**
     * 返回 combo 过滤条件
     * @param columns
     * @param value
     * @return
     */
    public static String getSqlWhereComboQStr(List<String> columns, Object value){
        boolean flag=false;//是否加 or
        StringBuilder sb=new StringBuilder();
        for(String str:columns){
            if(flag){
                sb.append(" or ");
            }
            sb.append(str+" like '%"+escapeSql(value.toString())+"%'");

            flag=true;
        }
        String retStr=sb.toString();
        System.out.println(retStr);
        return isEmpty(retStr)?"":"("+retStr+")";
    }

    public static String escapeSql(String sql){
        return sql.replaceAll("'", "''");
    }


    public static <T> String setToString(Set<T> set){
        StringBuilder builder = new StringBuilder();

        boolean flag=false;
        for(T t:set){
            if(flag){
                builder.append(",");
            }
            builder.append(t.toString());
            flag=true;
        }

        return builder.toString();
    }

}
